<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="动态标题" prop="title">
				<el-input v-model="form.title" style="width: 100%;" placeholder="动态内容" ></el-input>
			</el-form-item>
			<el-form-item label="动态内容" prop="content">
				<el-input v-model="form.content" placeholder="动态内容" clearable type="textarea" :autosize="{ minRows: 5, maxRows: 5}"></el-input>
			</el-form-item>
			<sc-upload-multiple
				:apiObj="apiObj"
				v-model="form.attachId" draggable :limit="9" tip="最多上传9个文件,单个文件不要超过10M,请上传图像格式文件"></sc-upload-multiple>
			<el-form-item label="开启评论" prop="commFlag">
				<el-switch v-model="form.commFlag" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">确 定</el-button>
		</template>
	</el-dialog>

</template>
<script>
export default {
	emits: ['success', 'closed'],
	components: {
	},
	data() {
		return {
			dialog: {
				isShow: false
			},
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			mode: "add",
			titleMap: {
				add: '发布动态信息',
				edit: '编辑动态信息',
				show: '查看'
			},
			apiObj: this.$API.file.uploadImg,
			form: {
				recordId: '',
				title: '',
				content: '',
				attachId: "",
				commFlag: "1",

			},
			options: [{
				value: '0',
				label: '请选择'
			}],
			rules: {
				title: [
					{required: true, message: '动态标题不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.info.dynamics.insertDynamicsRecord.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.info.dynamics.updateDynamicsRecord.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
					this.visible = false;
				}
			})
		},
		setData(data) {
			this.form.recordId = data.recordId
			this.form.title = data.title
			this.form.content = data.content
			this.form.attachId = data.attachId
			this.form.commFlag = data.commFlag
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
